package com.liyongzhen.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.liyongzhen.mapper.OperateLogMapper;
import com.liyongzhen.pojo.OperateLog;
import com.liyongzhen.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Slf4j
@Component
@Aspect//切面类
public class LogAspect {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(com.liyongzhen.anno.Log)")
    private Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {
        //操作人ID
        //获取请求头中的jwt令牌，然后解析令牌
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        Integer operateUser= (Integer) claims.get("id");

        //操作时间
        LocalDateTime opearteTime = LocalDateTime.now();
        //操作类名
        String className = joinPoint.getTarget().getClass().getName();
        //操作方法名
        String methodName = joinPoint.getSignature().getName();
        //操作方法参数
        Object[] args = joinPoint.getArgs();
        String methodParams= Arrays.toString(args);

        //调用目标 方法运行
        long begin=System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end=System.currentTimeMillis();
        //方法返回值
        String returnValues = JSONObject.toJSONString(result);
        //操作耗时
        LocalDateTime beginTime = LocalDateTime.now();
        long costTime=end-begin;


        //记录操作日志，
        OperateLog operateLog=new OperateLog(null,operateUser,opearteTime,className,methodName,methodParams,returnValues,costTime);
        operateLogMapper.insert(operateLog);

        log.info("AOP记录的日志{}",operateLog);
        return result;

    }
}
